package com.imooc.controller;

import com.google.gson.Gson;
import com.imooc.base.BaseInfoProperties;
import com.imooc.bo.ModifyUserBO;
import com.imooc.pojo.Users;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.UsersService;
import com.imooc.utils.JwtUtils;
import com.imooc.vo.UsersVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * App端用户Controller
 * @author liansen
 * @create 03-20-8:30
 */
@RestController
@RequestMapping("userinfo")
public class UserInfoController extends BaseInfoProperties {

    @Autowired
    private UsersService usersService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //app端用户更新信息
    @PostMapping("/modify")
    public GraceJSONResult modifyUserInfo(@RequestBody ModifyUserBO modifyUserBO){
        //修改用户信息
        usersService.modifyUserInfo(modifyUserBO);
        //返回最新用户信息

        UsersVO usersVO = getUserInfo(modifyUserBO.getUserId(), true);
        return GraceJSONResult.ok(usersVO);
    }

    //获取用户信息
    private UsersVO getUserInfo(String userId, boolean needJwt){
        Users users = usersService.getUserInfo(userId);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);

        if(needJwt){
            String uToken = jwtUtils.createJWTWithPrefix(new Gson().toJson(users), TOKEN_USER_PREFIX);
            usersVO.setUserToken(uToken);
        }

        return usersVO;
    }


    /**
     * 通过公司id获取hr数量
     * @param companyId
     * @return
     */
    @PostMapping("/getCountsByCompanyId")
    public GraceJSONResult getCountsByCompanyId(@RequestParam("companyId") String companyId){
        String redisJson = redisTemplate.opsForValue().get(REDIS_COMPANY_HR_COUNTS + ":" + companyId);
        Long hrCounts = 0L;
        if(StringUtils.isBlank(redisJson)){
            hrCounts = usersService.getCountsByCompanyId(companyId);
            //过期时间5分钟
            redisTemplate.opsForValue().set(REDIS_COMPANY_HR_COUNTS + ":" + companyId, new Gson().toJson(hrCounts), 5 * 60, TimeUnit.SECONDS);
        }else {
            hrCounts = Long.valueOf(redisJson);
        }
        return GraceJSONResult.ok(hrCounts);
    }

    @PostMapping("/bindingHRToCompany")
    public GraceJSONResult bindingHRToCompany(@RequestParam("hrUserId") String hrUserId,
                                                @RequestParam("realname") String realname,
                                                @RequestParam("companyId") String companyId){
        usersService.updateUserCompanyId(hrUserId, realname, companyId);
        Users user = usersService.getById(hrUserId);
        return GraceJSONResult.ok(user.getMobile());
    }

    /**
     * app端刷新用户信息
     */
    @PostMapping("/freshUserInfo")
    public GraceJSONResult freshUserInfo(@RequestParam("userId") String userId){
        UsersVO userInfo = getUserInfo(userId, true);
        return GraceJSONResult.ok(userInfo);
    }


    @PostMapping("/get")
    public GraceJSONResult get(@RequestParam("userId") String userId){
        UsersVO userInfo = getUserInfo(userId, false);
        return GraceJSONResult.ok(userInfo);
    }


    @PostMapping("/changeUserToHR")
    public GraceJSONResult changeUserToHR(@RequestParam("userId") String userId){
        usersService.changeUserToHR(userId);
        return GraceJSONResult.ok();
    }


}








